import { Controller, Post, UseGuards, Body, Req } from '@nestjs/common'
import { ApiBearerAuth, ApiConsumes, ApiOperation, ApiTags } from '@nestjs/swagger'
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'
import { FavoriteService } from './favorite.service'
import { Request } from 'express'
import { addFavoriteDto } from './dto/add-favorite.dto'
import { listFavoriteDto } from './dto/list-favorite.dto'
import { RateLimit } from '../common/rateLimit/rate-limit.decorator'

@ApiTags('menu')
@ApiBearerAuth('JWT')
@Controller('menu')
@UseGuards(JwtAuthGuard)
export class FavoriteController {
  constructor(private readonly favoriteService: FavoriteService) { }

  // 获取用户收藏列表
  @ApiOperation({ summary: '收藏菜品列表' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(30, 60 * 1000)
  @Post('/favorite/list')
  async getFavoriteList(@Body() data: listFavoriteDto, @Req() req: Request) {
    return this.favoriteService.getFavoriteList(req.user?.id as number, data)
  }

  @ApiOperation({ summary: '收藏菜品' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(15, 60 * 1000)
  @Post('/favorite/:menu_id')
  async addFavorite(@Body() data: addFavoriteDto, @Req() req: Request) {
    await this.favoriteService.addFavorite(req.user?.id as number, data.menu_id)
    return {
      isAdd: true
    }
  }

  @ApiOperation({ summary: '取消收藏' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(15, 60 * 1000)
  @Post('/cancelFavorite/:menu_id')
  async removeFavorite(@Body() data: addFavoriteDto, @Req() req: Request) {
    await this.favoriteService.removeFavorite(req.user?.id as number, data.menu_id)
    return {
      isAdd: true
    }
  }
}